package com.qianchang.microfinance.view.credit.data;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.dm.library.adapter.CommonAdapter;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.DMRecyclerView;
import com.dm.library.widgets.element.DButton;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.AttachmentListBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 附件上传
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/14 14:35
 */
public class CompleteMaterialActivity extends BaseActivity implements CommonAdapter.OnItemClickListener {

    private static final String TAG = "CompleteMaterialActivity";
    @BindView(R.id.recycler_complete_material)
    DMRecyclerView recyclerCompleteMaterial;
    @BindView(R.id.btn_commit)
    DButton btnCommit;

    private CompleteMaterialAdapter completeMaterialAdapter;
    ArrayList<AttachmentListBean> completeMaterialList = new ArrayList<>();
    private String currentId;
    private String productType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_complete);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        findPerfectInfo();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_complete_material));
        recyclerCompleteMaterial.setLayoutManager(new LinearLayoutManager(this));
        completeMaterialAdapter = new CompleteMaterialAdapter(this, completeMaterialList);
        recyclerCompleteMaterial.setAdapter(completeMaterialAdapter);
        recyclerCompleteMaterial.setHasFixedSize(true);
        recyclerCompleteMaterial.setAutoLoadMoreEnable(true);
        recyclerCompleteMaterial.setEmptyText(getString(R.string.no_data_to_load));
        completeMaterialAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (bundle != null) {
            currentId = bundle.getString(ConstantAppInfo.PROJECT_ID);
            productType = bundle.getString(ConstantAppInfo.PROJECT_TYPE);
        }
    }

    private void findPerfectInfo() {
        HttpParams params = new HttpParams();
        show();
        params.put("productType", productType);
        params.put("projectId", currentId);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_PRODUCT_DATA_LIST, params, new RequestCallBack<List<AttachmentListBean>>() {
            @Override
            public void onSuccess(JsonResult<List<AttachmentListBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    if (response.getObject() != null) {
                        completeMaterialList.clear();
                        completeMaterialList.addAll(response.getObject());
                        recyclerCompleteMaterial.notifyMoreFinish(false);
                    } else {
                        ToastUtil.getInstant().show(CompleteMaterialActivity.this, getString(R.string.data_error));
                    }
                } else {
                    ToastUtil.getInstant().show(CompleteMaterialActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(CompleteMaterialActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }

        });
    }


    @Override
    public void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        AttachmentListBean attachmentListBean = completeMaterialList.get(position);
        bundle.putSerializable(CompleteMaterialDetailActivity.MATERIAL_BEAN, attachmentListBean);
        bundle.putString(ConfirmInfoJinbaoActivity.CURRENT_ID, currentId);
        startActivity(CompleteMaterialDetailActivity.class, bundle);
    }

    @OnClick(R.id.btn_commit)
    public void onViewClicked() {
        submit();
    }

    private void submit() {
        boolean canCommit = false;
        for (AttachmentListBean attachmentListBean : completeMaterialList) {
            if (attachmentListBean.getIsAuth() == 0) {
                canCommit = true;
                break;
            }
        }
        if (canCommit) {
            ToastUtil.getInstant().show(CompleteMaterialActivity.this, "请提交所有附件信息");
            return;
        }
        HttpParams params = new HttpParams();
        show();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.SUBMIT_ATTACHMENT_LIST, params, new RequestCallBack<List<AttachmentListBean>>() {
            @Override
            public void onSuccess(JsonResult<List<AttachmentListBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    setResult(RESULT_OK);
                    finish();
                } else {
                    ToastUtil.getInstant().show(CompleteMaterialActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(CompleteMaterialActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }
}
